<?php
/**
 * Created by PhpStorm.
 * User: Zhoujinda
 * Date: 2016/5/25
 * Time: 17:00
 */
namespace Mengwu\Controller;

use Mengwu\Api\MwApi;

class MengwuApiController extends BaseStoreController{
    protected $api_ver; //客户端版本号
    protected $notify_id;
    protected $alg;

    /**
     *购物车分页查询
     */
    public function spcart_query(){

        if(IS_POST){
            $this->needLogin();

            $data = [
                'uid' => $this->getUID(),
                'api_ver' => 101
            ];

            $result = mwApi(MwApi::BY_ShoppingCart_all,$data);

            $PICTURE_REMOTE_URL = C('PICTURE_REMOTE_URL');
            if($result['status']){
                $store_list = $result['info']['store_list'];
                if(empty($store_list)){
                    $this->ajaxReturnSuc(['carts'=>[]]);
                }
                foreach($store_list as &$val){
                    foreach($val['carts'] as &$val2){
                        $val2['icon_url'] = $PICTURE_REMOTE_URL.$val2['icon_url'].'&size=120';
                    }
                }
                $this->ajaxReturnSuc($store_list[0]);
            }else{
                $this->ajaxReturnErr($result['info']);
            }
        }
        
    }

    /**
     * 购物车删除商品
     */
    public function spcart_delete(){

        if(IS_POST){
            $this->needLogin();

            //比对购物车UID

            $data = [
                'uid' => $this->getUID(),
                'id' => I('id')
            ];

            $result = mwApi(MwApi::BY_ShoppingCart_delete,$data);

            $this->apiReturn($result,null,'error');

        }
    }

    /**
     * 购物车添加商品
     */
    public function spcart_add(){

        if(IS_POST){
            $this->needLogin();

            $data = [
                'api_ver' => 101,
                'uid' => $this->getUID(),
                'pid' => I('pid'),
                'count' => I('count'),
                'psku_id' => I('psku_id'),
                'group_id' => I('group_id')
            ];

            $result = mwApi(MwApi::BY_ShoppingCart_add,$data);

            $this->apiReturn($result,null);
        }
    }
    /**
     * 购物车修改商品
     */
    public function spcart_edit(){

        if(IS_POST){
            $this->needLogin();

            $data = [
                'uid' => $this->getUID(),
                'id' => I('id'),
                'count' => I('count')
            ];

            $result = mwApi(MwApi::BY_ShoppingCart_edit,$data);

            $this->apiReturn($result,null);
        }
    }

    /**
     * 商品咨询保存
     */
    public function sp_faq(){

        if(IS_POST){
            $this->needLogin();

            $data = [
                'ask_uid' => $this->getUID(),
                'pid' => I('pid'),
                'ask_content' => I('content')
            ];

            $result = mwApi(MwApi::BY_Product_ask,$data);

            $this->apiReturn($result,null);
        }

    }

    /**
     * 商品咨询查询
     */
    public function sp_query_faq(){

        if(IS_POST){

            $data = [
                'pid' => I('pid'),
                'page_no' => I('page_no',1),
                'page_size' => I('page_size',10)
            ];

            $result = mwApi(MwApi::BY_Product_queryFaq,$data);

            if($result['status']){
                foreach($result['info']['list'] as &$val){
                    $val['ask_head'] = getHeadUrl($val['ask_head'],120);
                    $val['reply_head'] = getHeadUrl($val['reply_head'],120);
                }
            }

            $this->apiReturn($result,null);
        }
    }


    /**
     * 收货地址查询
     */
    public function address_query(){

        if(IS_POST){
            $this->needLogin();

            $data = [
                'uid' => $this->getUID()
            ];

            $result = mwApi(MwApi::BY_Address_queryNoPaging,$data);

            if($result['status']){
                $info = [];
                foreach($result['info'] as $val){
                    $a = [
                        'id' => $val['id'],
                        'contactname' => $val['contactname'],
                        'address' => [
                            $val['province']
                        ],
                        'addressid' => [$val['provinceid'], $val['cityid'], $val['areaid']],
                        'detail' => $val['detailinfo'],
                        'postcode' => $val['postal_code'],
                        'phone' => $val['mobile'],
                        'default' => $val['default']
                    ];
                    if(trim($val['city'])!=""){
                        array_push($a['address'], $val['city']);
                    }
                    if(trim($val['area']!="")){
                        array_push($a['address'], $val['area']);
                    }
                    array_push($info,$a);
                }
                $result['info'] = $info; //转换为前台格式
            }

            $this->apiReturn($result,null,'error');
        }

    }

    /**
     * 收货地址添加
     */
    public function address_add(){

        if(IS_POST){
            $this->needLogin();

            $data = [
                'api_ver' => 101,
                'uid' => $this->getUID(),
                'country' => 1017,
                'provinceid' => I('provinceid'),
                'cityid' => I('cityid',0),
                'areaid' => I('areaid',0),
                'detailinfo' => I('detailinfo'),
                'contactname' => I('contactname'),
                'mobile' => I('mobile'),
                'postal_code' => I('postal_code','0000'),
                'province' => I('province'),
                'city' => I('city',' '),
                'area' => I('area',' '),
                'default' => I('default')
            ];

            $result = mwApi(MwApi::BY_Address_add,$data);

            $this->apiReturn($result,null,'error');
        }

    }

    /**
     * 收货地址更新
     */
    public function address_update(){

        if(IS_POST){
            $this->needLogin();

            $data = [
                'id' => I('id'),
                'uid' => $this->getUID(),
                'country' => 1017,
                'provinceid' => I('provinceid'),
                'cityid' => I('cityid',0),
                'areaid' => I('areaid',0),
                'detailinfo' => I('detailinfo'),
                'contactname' => I('contactname'),
                'mobile' => I('mobile'),
                'postal_code' => I('postal_code','0000'),
                'province' => I('province'),
                'city' => I('city',' '),
                'area' => I('area',' '),
                'default' => I('default')
            ];

            $result = mwApi(MwApi::BY_Address_update,$data);

            $this->apiReturn($result,null,'error');
        }

    }

    /**
     * 收货地址删除
     */
    public function address_delete(){

        if(IS_POST){
            $this->needLogin();

            $data = [
                'id' => I('id'),
                'uid' => $this->getUID()
            ];

            $result = mwApi(MwApi::BY_Address_delete,$data);

            $this->apiReturn($result,null,'error');
        }

    }

    /**
     * 订单生成
     */
    public function order_add(){

        if(IS_POST){
            $this->needLogin();

            $data = [
                'api_ver' => 101,
                'uid' => $this->getUID(),
                'cart_ids' => I('cart_ids'),
                'idcode' => I('idcode'),
                'note' => I('node'),
                'address_id' =>I('address_id'),
                'from' => I('from',1)
            ];

            $result = mwApi(MwApi::BY_Orders_add,$data);

            $this->apiReturn($result,null);
        }

    }

    /**
     * 订单取消
     */
    public function order_cancel(){

        if(IS_POST){
            $this->needLogin();
            $data = [
                'uid' => $this->getUID(),
                'order_code' => I('order_code')
            ];

            $result = mwApi(MwApi::BY_Orders_cancel,$data);

            $this->apiReturn($result,null);
        }

    }

    /**
     * 订单确认
     */
    public function order_sure(){

        if(IS_POST){
            $this->needLogin();
            $data = [
                'uid' => $this->getUID(),
                'order_code' => I('order_code')
            ];

            $result = mwApi(MwApi::BY_Orders_cancel,$data);

            $this->apiReturn($result,null);
        }

    }

    /**
     * 订单计算运费-商品
     */
    public function order_freight_cost(){

        if(IS_POST){
            $this->needLogin();
            $from = I('from');
            if($from=='spcart'){
                $data = [
                    'uid' => $this->getUID(),
                    'address_id' =>I('address_id'),
                    'cart_ids' => I('cart_ids')
                ];

                $result = mwApi(MwApi::BY_Freight_cost,$data);

                $this->apiReturn($result,null);
            }
            if($from=='buy'){
                $data = [
                    'uid' => $this->getUID(),
                    'address_id' =>I('address_id'),
                    'pid' => I('pid'),
                    'count' => I('count')
                ];

                $result = mwApi(MwApi::BY_Freight_costSku,$data);
                $this->apiReturn($result,null);
            }
            exit('INVALID CODE');



        }

    }

    /**
     * 红包查询不分页
     */
    public function redEnvelope_query(){

        if(IS_POST){
            $this->needLogin();

            $data = [
                'api_ver' => 101,
                'uid' => $this->getUID(),
                'is_use' =>I('is_use'),
                'is_expire' => I('is_expire')
            ];

            $result = mwApi(MwApi::BY_RedEnvelope_query,$data);

            $this->apiReturn($result,null);
        }
    }

    /**
     * 购物券查询不分页
     */
    public function coupon_query(){

        if(IS_POST){
            $this->needLogin();

            $data = [
                'uid' => $this->getUID(),
                'is_use' =>I('is_use'),
                'is_expire' => I('is_expire')
            ];

            $result = mwApi(MwApi::BY_Coupon_query,$data);

            $this->apiReturn($result,null);
        }

    }


    /**
     * 帖子标签查询
     */
    public function post_tag_query(){

        if(IS_POST){

            $data = [
                'uid' => 0, //暂未使用
                'sort' =>0, //0=>随机,1=>最新,2=>最热
                'number' => I('number',10)  //数量默认10个
            ];

            $result = mwApi(MwApi::BY_Tag_finds,$data);

            if($result['status']){
                $new = [];
                foreach($result['info'] as $val){
                    $new[] = [
                        'name'=> $val['name'],
                        'hot' => $val['used'] >=50 ? true : false
                    ];
                }
                $result['info'] = $new;
            }

            $this->apiReturn($result,null);
        }
    }

    /**
     * 帖子标签添加
     */
    public function post_tag_add(){

        if(IS_POST){
            $this->needLogin();

            $data = [
                'uid' => $this->getUID(),
                'tag_name' =>I('tag_name'), //标签组，多个英文逗号分隔，仅中文字母数字
                'tid' => I('tid')  //帖子ID
            ];

            $result = mwApi(MwApi::BY_Tag_addAll,$data);

            $this->apiReturn($result,null);
        }
    }

    /**
     * 帖子发表(育儿经)
     */
    public function yuerjing_post_add($type=false){

        if(IS_POST){
            $this->needLogin();

            $fid = 0;
            if($type=='share')$fid = 7; //妈妈分享
            if($type=='question')$fid = 8; //妈妈提问
            if($fid==0)return;
            $data = [
                'api_ver' => 101,
                'fid' => $fid,
                'author_id' => $this->getUID(),
                'subject' => I('subject'),
                'message' => I('message'),
                'attachments' => I('attachments'),
                'tags' => I('tags')
            ];

            $result = mwApi(MwApi::BY_ForumThread_add,$data);

            $this->apiReturn($result,null);
        }
    }

    /**
     * 帖子查询(育儿经)
     */
    public function yuerjing_post_query($type=false){

        if(IS_POST){

            $fid = 0;
            if($type=='share')$fid = 7; //妈妈分享
            if($type=='question')$fid = 8; //妈妈提问
            if($fid==0)return;
            $data = [
                'fid' => $fid,
                'page_no' => I('page_no'),
                'page_size' => I('page_size'),
                'img_num' => 5,
                'tag_id' => I('tag_id'),
                'uid' => $this->getUID(),
                'max_reply' => 1,
                'max_comment' => 1,
                'max_like' => 1
            ];
            
            if(empty($data['tag_id'])){
                unset($data['tag_id']);
                $result = mwApi(MwApi::BY_ForumThread_query,$data);
            }else{
                $result = mwApi(MwApi::BY_ForumThread_tag,$data);
            }
            
            if($result['status']){
                $PICTURE_REMOTE_URL = C('PICTURE_REMOTE_URL');
                foreach($result['info']['list'] as &$val){
                    foreach($val['img'] as &$x){
                        $x = $PICTURE_REMOTE_URL.$x;
                    }
                    if(strlen($val['message'])>240)$val['message'] = mb_substr($val['message'], 0, 240, 'utf8').'......';
                    $val['head'] = getHeadUrl($val['head'],120);
                }
            }

            $this->apiReturn($result,null);
        }
    }

    /**
     * 帖子和回复点赞
     */
    public function post_like(){
        
        if(IS_POST){
            $this->needLogin();
            
            $data = [
                'api_ver' => 101,
                'tid' => I('tid'),
                'uid' => $this->getUID(),
                'pid' => I('pid'),
                'like_value' => 1
            ];

            $result = mwApi(MwApi::BY_ForumThread_like,$data);

            $this->apiReturn($result,null);
            
        }
        
    }

    /**
     * 帖子是否收藏
     */
    public function post_favorites(){

        if(IS_POST){
            $this->needLogin();

            $data = [
                'tid' => I('tid'),
                'uid' => $this->getUID(),
                'favorite_value' => I('favorite_value'),
            ];

            $result = mwApi(MwApi::BY_ForumThread_favorites,$data);

            $this->apiReturn($result,null);

        }

    }

    /**
     * 育儿经评论回复
     */
    public function yuerjing_post_replyPost($type=false){

        if(IS_POST){
            $this->needLogin();
            $fid = 0;
            if($type=='share')$fid = 7; //妈妈分享
            if($type=='question')$fid = 8; //妈妈提问
            if($fid==0)return;

            $data = [
                'pid' => I('pid'),
                'tid' => I('tid'),
                'author_id' => $this->getUID(),
                'to_uid' => I('to_uid'),
                'fid' => $fid,
                'comment' => I('comment'),
            ];

            $result = mwApi(MwApi::BY_ForumPost_replyPost,$data);

            $this->apiReturn($result,null);

        }
        
    }
    /**
     * 育儿经帖子回复
     */
    public function yuerjing_post_reply($type=false){

        if (IS_POST) {
            $this->needLogin();
            $fid = 0;
            if($type=='share')$fid = 7; //妈妈分享
            if($type=='question')$fid = 8; //妈妈提问
            if($fid==0)return;

            $data = [
                'fid' => $fid,
                'tid' => I('tid'),
                'author_id' => $this->getUID(),
                'message' => I('message'),
            ];

            $result = mwApi(MwApi::BY_ForumPost_reply, $data);

            $this->apiReturn($result, null);

        }
    }

    /**
     * 个人中心-百宝箱育儿经
     */
    public function baibaoxiang_yuerjing($type=false){
        if(IS_POST){
            $fid = 0;
            if($type=='share')$fid = 7; //妈妈分享
            if($type=='question')$fid = 8; //妈妈提问
            if($fid==0)return;

            $data = [
                'page_no' => I('page_no',1),
                'page_size' => I('page_size',10),
                'uid' => $this->getUID(),
                'img_num' => 1,
                'fid' => $fid,
                'max_reply' => 1,
                'max_commont' => 1
            ];

            $result = mwApi(MwApi::BY_BbsUserCenter_queryThread,$data);
            if($result['status']){
                foreach($result['info']['list'] as &$val){
                    $val['head'] = getHeadUrl($val['head'],120);
                    if(mb_strlen($val['message'],'utf-8')>190){
                        $val['message'] = mb_substr($val['message'],0,190,'utf-8').'...';
                    }else{
                        $val['message'] = mb_substr($val['message'],0,190,'utf-8');
                    }
                }
            }
            $this->apiReturn($result, null);
        }

    }

    /**
     * 关注用户
     */
    public function User_focus(){

        if (IS_POST) {
            $this->needLogin();

            $data = [
                'uid' => $this->getUID(),
                'focus_uid' => I('focus_uid'),
                'value' => I('value')
            ];

            if($data['focus_uid'] == $data['uid'] ){
                $this->ajaxReturnErr('不能关注自己');
            }

            $result = mwApi(MwApi::BY_User_focus, $data);

            $this->apiReturn($result, null);

        }
    }

    /**
     * 商品评价分页
     */
    public function spdetail_comment(){

        if(IS_POST){

            $data = [
                'pid' => I('pid'),
                'page_no' => I('page_no'),
                'page_size' => I('page_size'),
                'max_img' => 5
            ];

            $result = mwApi(MwApi::BY_Product_comment, $data);

            if($result['status']){
                $PICTURE_REMOTE_URL = C('PICTURE_REMOTE_URL');
                $comment = $result['info'];

                foreach($comment['list'] as &$val){
                    $val['user_head'] = getHeadUrl($val['user_head'],60);
                    //图片处理
                    foreach($val['img'] as &$vo){
                        $vo = $PICTURE_REMOTE_URL.$vo.'&size=60';
                    }
                }
                $this->ajaxReturnSuc($comment);

            }else{
                $this->ajaxReturnErr($result['info']);
            }
        }
    }

    /**
     * 商品收藏
     */
    public function product_favorites(){

        if(IS_POST){
            $this->needLogin();

            $data = [
                'pid' => I('pid'),
                'uid' => $this->getUID(),
                'favorite_value' => I('value'),
                'fav_type' => 1
            ];

            $result = mwApi(MwApi::BY_Product_favorites, $data);
            $this->apiReturn($result, null);
        }

    }

    /**
     * 赠无限收藏商品分页
     */
    public function zengwuxian_favorites(){

        if(IS_POST){
            $this->needLogin();

            $data = [
                'pid' => I('pid'),
                'uid' => $this->getUID(),
                'favorite_value' => I('value'),
                'fav_type' => 3
            ];

            $result = mwApi(MwApi::BY_Product_favorites, $data);
            $this->apiReturn($result, null);
        }

    }

    /**
     *商品收藏分页
     */
    public function product_favorites_gift(){

        if(IS_POST){
            $this->needLogin();

            $data = [
                'page_no' => I('page_no'),
                'uid' => $this->getUID(),
                'page_size' => I('page_size',10),
                'max_img' => 3
            ];

            $result = mwApi(MwApi::BY_Product_queryFavGift, $data);

            if($result['status']){
                $PICTURE_REMOTE_URL = C('PICTURE_REMOTE_URL');
                $sp = $result['info'];
                foreach($sp['list'] as &$val){
                    $val['head'] = getHeadUrl($val['head'],120);
                    //图片处理
                    foreach($val['show_imgs'] as &$vo){
                        $vo = $PICTURE_REMOTE_URL.$vo.'&size=200';
                    }
                }
                $this->ajaxReturnSuc($sp);
            }else{
                $this->ajaxReturnErr($result['info']);
            }
        }
    }

    /**
     * 赠无限列表分页
     */
    public function product_listGift(){

        if(IS_POST){
            $this->needLogin();

            $type = I('type');

            $data = [
                'page_no' => I('page_no'),
                'page_size' => I('page_size',10)
            ];

            if($type=='send'){
                $data['uid'] = $this->getUID();
            }
            if($type=='receive'){
                $data['to_uid'] = $this->getUID();
            }

            $result = mwApi(MwApi::BY_Product_listGift, $data);

            if($result['status']){
                $PICTURE_REMOTE_URL = C('PICTURE_REMOTE_URL');
                $sp = $result['info'];
                foreach($sp['list'] as &$val){
                    $val['head'] = getHeadUrl($val['head'],120);
                    //图片处理
                    foreach($val['show_imgs'] as &$vo){
                        $vo = $PICTURE_REMOTE_URL.$vo.'&size=200';
                    }
                }
                $this->ajaxReturnSuc($sp);
            }else{
                $this->ajaxReturnErr($result['info']);
            }
        }
    }

    /**
     * 消息删除
     */
    public function message_delete(){

        if(IS_POST){
            $this->needLogin();
            $ids = I('id');
            if(empty($ids)) $this->ajaxReturnErr('id不能为空');

            $data = [
                'api_ver' => 101,
                'ids' => I('id'),
                'uid' => $this->getUID(),
            ];

            $result = mwApi(MwApi::BY_Message_delete, $data);

            if($result['status']){
                foreach($result['info']['list'] as &$val){
                    $val['head'] = getHeadUrl($val['head'],120);
                }
            }

            $this->apiReturn($result,null);
        }

    }

    /**
     * 点赞我的
     */
    public function likeMe(){

        if(IS_POST){
            $this->needLogin();

            $data = [
                'uid' => $this->getUID(),
                'page_no' => I('page_no',1),
                'page_size' => I('size',5)
            ];

            $result = mwApi(MwApi::BY_BbsUserCenter_likeMe, $data);

            $this->apiReturn($result,null);

        }

    }

    /**
     * 订单评价
     */
    public function order_comment(){
        
        if(IS_POST){
            
            $this->needLogin();
            
            $data = [
                'api_ver' => 101,
                'uid' => $this->getUID(),
                'pid' => I('pid'),
                'order_code' => I('order_code'),
                'score' => 5,
                'comment' => I('comment'),
                'attachments' => I('attachments'),
                'psku_id' => I('psku_id'),
                'group_id' => I('group_id'),
                'package_id' => I('package_id')
            ];
            
            $result = mwApi(MwApi::BY_Orders_comment,$data);

            $this->apiReturn($result,null);
            
        }
        
    }






//    public function addUsers(){
//        ignore_user_abort(true);
//        set_time_limit(0);
//        $num = intval($_GET["num"]);
//        $type = 1;
//        $from = 0;
//        $suc  = 0;
////        $trans = new \think\model\Adv();
////        $trans->startTrans();
//        $flag = false;
//        $now  = time();
//        for ($i=1; $i < $num ; $i++) {
//            $username = "M_STEST2_".$i;
//            $password = "e56d09a4e36a3281d98176728bf24683";
//            $nickname = $username;
//            //写入第一张表 UcenterMember
//            $model = new UcenterMemberModel();
//            $entity = [
//                'username'        =>$username,
//                'password'        =>$password,
//                'mobile'          =>'',
//                'email'           =>'',
//                'reg_time'        =>$now,
//                'reg_ip'          =>0,
//                'last_login_time' =>0,
//                'last_login_ip'   =>0,
//                'update_time'     =>$now,
//                'status'          =>1,
//                'reg_from'        =>0,
//                'weixin_uid'      =>'',
//            ];
//            $uid = $model ->add($entity);
//            // dump($uid);
//            //写入第二张表 common_member
//            $model2 = new MemberModel();
//            $map = [
//                'uid'         => $uid,
//                'realname'    => '',
//                'idnumber'    => '',
//                'nickname'    => $nickname,
//                'sex'         => 0,
//                'birthday'    => $now,
//                'qq'          => '',
//                'head'        => 0,
//                'score'       => 0,
//                'login'       => 0,
//                'update_time' => $now, //auto-model bug
//                'status'      => 1,    //auto-model bug
//                'add_uid'     => 0,
//                'sign'        => '',
//            ];
//            $r = $model2 ->add($map);
//            dump($r);
//            //插入到第三张表
//            $model3 = new MemberConfigModel();
//            $map = [
//                'uid'               =>$uid,
//                'phone_validate'    =>0,
//                'email_validate'    =>0,
//                'idcode'            =>'',
//                'wxopenid'          =>'',
//                'identity_validate' =>0,
//                'invite_id'         =>'',
//            ];
//            $model3 ->add($map);
//            //插入到第四张表
//            $model4 = new UserMemberModel();
//            $map = [
//                'uid'           =>$uid,
//                'group_id'      =>1,
//                'active_status' =>0,
//                'active_time'   =>$now,
//            ];
//            $model4 ->add($map);
//            echo "添加用户".$uid.":".$username."成功<br/>";
//        }
//        exit('结束');
//    }



}